package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.FriendsLink;
import com.flymobi.util.QL;

public class FriendsLinkService extends CommonService {

	public List<FriendsLink> list(int startRow, int pagePerSize) {
		return dao.find(FriendsLink.class, QL.HQL.FriendsLink.LIST, new Object[] {},startRow, pagePerSize);
	}
	
	public Long getCount() {
		return dao.getCount(QL.HQL.FriendsLink.GET_COUNT, new Object[] {});
	}

	public void save(FriendsLink FriendsLink) {
		dao.save(FriendsLink);
	}

	public FriendsLink load(String id) {
		return dao.load(FriendsLink.class, new Long(id));
	}
	
	public FriendsLink get(String id) {
		return dao.get(FriendsLink.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
	
	public void updateUp(String id) {
		FriendsLink pe = this.get(id);
		FriendsLink peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}
	
	public void updateDown(String id) {
		FriendsLink pe = this.get(id);
		FriendsLink peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(FriendsLink pe, FriendsLink peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private FriendsLink findUp(FriendsLink pe) {
		return dao.findSingle(FriendsLink.class, QL.HQL.FriendsLink.LIST_UP, new Object[] {pe.getIdx() });
	}
	
	private FriendsLink findDown(FriendsLink pe) {
		return dao.findSingle(FriendsLink.class, QL.HQL.FriendsLink.LIST_DOWN, new Object[] {pe.getIdx() });
	}
	
	public Long getSeq(){
		return dao.getSeq(QL.HQL.FriendsLink.GET_MAX_IDX,null);
	}
}
